package org.ruwang.chinamobileai.config;

import org.ruwang.chinamobileai.constants.Constant;
import org.ruwang.chinamobileai.tools.PackageDataTools;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.ai.chat.client.advisor.SimpleLoggerAdvisor;
import org.springframework.ai.chat.client.advisor.api.Advisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.memory.InMemoryChatMemory;
import org.springframework.ai.embedding.EmbeddingModel;
import org.springframework.ai.vectorstore.SimpleVectorStore;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class SpringAiConfig {

    @Bean
    public ChatMemory chatMemory(){
        return new InMemoryChatMemory();
    }

    @Bean
    public Advisor simpleLoggerAdvisor(){
        return new SimpleLoggerAdvisor();
    }

    @Bean
    public Advisor messageChatMemoryAdvisor(ChatMemory chatMemory){
        return new MessageChatMemoryAdvisor(chatMemory);
    }

    @Bean
    public VectorStore vectorStore(EmbeddingModel embeddingModel){
        return SimpleVectorStore.builder(embeddingModel).build();
    }

    @Bean
    public ChatClient chatClient(
            ChatClient.Builder builder
            ,Advisor simpleLoggerAdvisor
            ,Advisor messageChatMemoryAdvisor
            ,PackageDataTools PackageDataTools
            ){
        return  builder
                .defaultSystem(Constant.SYSTEM_ROLE)
                .defaultAdvisors(simpleLoggerAdvisor)
                .defaultAdvisors(messageChatMemoryAdvisor)
                .defaultTools(PackageDataTools)
                .build();
    }
}
